home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / python / menu.py < prev    next >
Encoding:
Python Source  |  2007-11-12  |  4.8 KB  |  133 lines

  1. # Miro - an RSS based video player application
  2. # Copyright (C) 2005-2007 Participatory Culture Foundation
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  17.  
  18. from gettext import gettext as _
  19.  
  20. import app
  21. import eventloop
  22. import tabs
  23.  
  24. def makeMenu(items):
  25.     """Convenience function to create a list of MenuItems given on a list of
  26.     (callback, label) tuples.
  27.     """
  28.  
  29.     return [MenuItem(callback, label) for callback, label in items]
  30.  
  31. class MenuItem:
  32.     """A single menu item in a context menu.
  33.  
  34.     Normally frontends should display label as the text for this menu item,
  35.     and if it's clicked on call activate().  One second case is if label is
  36.     blank, in which case a separator should be show.  Another special case is
  37.     if callback is None, in which case the label should be shown, but it
  38.     shouldn't be clickable.  
  39.     
  40.     """
  41.  
  42.     def __init__(self, callback, label):
  43.         self.label = label
  44.         self.callback = callback
  45.  
  46.     def activate(self):
  47.         """Run this menu item's callback in the backend event loop."""
  48.  
  49.         eventloop.addUrgentCall(self.callback, "context menu callback")
  50.  
  51. def makeContextMenu(templateName, view, selection, clickedID):
  52.     if len(selection.currentSelection) == 1:
  53.         obj = selection.getObjects()[0]
  54.         if isinstance(obj, tabs.Tab):
  55.             obj = obj.obj
  56.         return obj.makeContextMenu(templateName, view)
  57.     else:
  58.         type = selection.getType()
  59.         objects = selection.getObjects()
  60.         if type == 'item':
  61.             return makeMultiItemContextMenu(templateName, view, objects,
  62.                     clickedID)
  63.         elif type == "playlisttab":
  64.             return makeMenu([
  65.                 (app.controller.removeCurrentPlaylist, _('Remove')),
  66.             ])
  67.         elif type == "channeltab":
  68.             return makeMenu([
  69.                 (app.controller.updateCurrentFeed, _('Update Channels Now')),
  70.                 (app.controller.removeCurrentFeed, _('Remove')),
  71.             ])
  72.         else:
  73.             return None
  74.  
  75. def makeMultiItemContextMenu(templateName, view, selectedItems, clickedID):
  76.     c = app.controller # easier/shorter to type
  77.     watched = unwatched = downloaded = downloading = available = uploadable = 0
  78.     for i in selectedItems:
  79.         if i.getState() == 'downloading':
  80.             downloading += 1
  81.         elif i.isDownloaded():
  82.             if i.downloader and i.downloader.getState() == 'finished' and i.downloader.getType() == 'bittorrent':
  83.                 uploadable += 1
  84.             downloaded += 1
  85.             if i.getSeen():
  86.                 watched += 1
  87.             else:
  88.                 unwatched += 1
  89.         else:
  90.             available += 1
  91.  
  92.     items = []
  93.     if downloaded > 0:
  94.         items.append((None, _('%d Downloaded Items') % downloaded))
  95.         items.append((lambda: c.playView(view, clickedID),
  96.             _('Play')))
  97.         items.append((c.addToNewPlaylist, _('Add to new playlist')))
  98.         if templateName in ('playlist', 'playlist-folder'):
  99.             label = _('Remove From Playlist')
  100.         else:
  101.             label = _('Remove From the Library')
  102.         items.append((c.removeCurrentItems, label))
  103.         if watched:
  104.             def markAllUnseen():
  105.                 for item in selectedItems:
  106.                     item.markItemUnseen()
  107.             items.append((markAllUnseen, _('Mark as Unwatched')))
  108.         if unwatched:
  109.             def markAllSeen():
  110.                 for item in selectedItems:
  111.                     item.markItemSeen()
  112.             items.append((markAllSeen, _('Mark as Watched')))
  113.  
  114.     if available > 0:
  115.         if len(items) > 0:
  116.             items.append((None, ''))
  117.         items.append((None, _('%d Available Items') % available))
  118.         items.append((app.controller.downloadCurrentItems, _('Download')))
  119.  
  120.     if downloading:
  121.         if len(items) > 0:
  122.             items.append((None, ''))
  123.         items.append((None, _('%d Downloading Items') % downloading))
  124.         items.append((app.controller.stopDownloadingCurrentItems, 
  125.             _('Cancel Download')))
  126.         items.append((app.controller.pauseDownloadingCurrentItems, 
  127.             _('Pause Download')))
  128.  
  129.     if uploadable > 0:
  130.         items.append((c.startUploads, _('Restart Upload')))
  131.  
  132.     return makeMenu(items)
  133.